package com.tjetc;

import com.tjetc.vo.AppDate;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ServerMain {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(AppDate.Port);
            ExecutorService pool = Executors.newFixedThreadPool(100);
            System.out.println("服务器启动");
            while (true) {
                Socket socket = serverSocket.accept();
                pool.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            System.out.println("连接成功");
                            ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                            ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                            String interfaceName=in.readUTF();
                            String methodName=in.readUTF();
                            Class[]paramTypes  = (Class[])in.readObject();
                            Object[]args = (Object[])in.readObject();
                            System.out.println("接口名:"+interfaceName);
                            System.out.println("方法名:"+methodName);
                            System.out.println("参数类型:"+paramTypes);
                            System.out.println("参数:"+args);
                            InputStream inputStream = ServerMain.class.getClassLoader().getResourceAsStream("server.properties");
                            Properties properties = new Properties();
                            properties.load(inputStream);
                            String property = properties.getProperty(interfaceName);
                            Class<?> ImplClass = Class.forName(property);
                            Object instance = ImplClass.getDeclaredConstructor().newInstance();
                            Method method = ImplClass.getDeclaredMethod(methodName, paramTypes);
                            Object result = method.invoke(instance, args);
                            out.writeObject(result);
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        } catch (InstantiationException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
